package com.sdhs.costcontrol.controller;

import cn.hutool.core.io.resource.ClassPathResource;
import com.sdhs.common.annotation.Log;
import com.sdhs.common.core.controller.BaseController;
import com.sdhs.common.core.domain.AjaxResult;
import com.sdhs.common.core.page.TableDataInfo;
import com.sdhs.common.enums.BusinessType;
import com.sdhs.common.utils.poi.TemplateExcelUtils;
import com.sdhs.costcontrol.domain.GcControlPlanList;
import com.sdhs.costcontrol.domain.GcControlPurchasePlan;
import com.sdhs.costcontrol.mapper.GcControlPlanListMapper;
import com.sdhs.costcontrol.service.IGcControlPurchasePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成本控制--采购计划Controller
 *
 * @author andong
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/costcontrol/purchase")
public class GcControlPurchasePlanController extends BaseController {
    @Autowired
    private IGcControlPurchasePlanService gcControlPurchasePlanService;

    @Autowired
    private GcControlPlanListMapper gcControlPlanListMapper;

    /**
     * 查询成本控制--采购计划列表
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(GcControlPurchasePlan gcControlPurchasePlan) {
        startPage();
        List<GcControlPurchasePlan> list = gcControlPurchasePlanService.selectGcControlPurchasePlanList(gcControlPurchasePlan);
        return getDataTable(list);
    }

    /**
     * 查询成本控制--采购确认列表
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:purchase:confirmationList')")
    @GetMapping("/confirmationList")
    public TableDataInfo confirmationList(GcControlPurchasePlan gcControlPurchasePlan) {
        startPage();
        List<GcControlPurchasePlan> list = gcControlPurchasePlanService.selectConfirmationList(gcControlPurchasePlan);
        return getDataTable(list);
    }

    /**
     * 导出成本控制--采购计划列表
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:purchase:export')")
    @Log(title = "成本控制--采购计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GcControlPurchasePlan gcControlPurchasePlan) {
        GcControlPurchasePlan controlPurchasePlan = gcControlPurchasePlanService.selectGcControlPurchasePlanById(gcControlPurchasePlan.getId());
        Map<String, Object> params = new HashMap<>();
        params.put("id", gcControlPurchasePlan.getId());
        params.put("projectId", gcControlPurchasePlan.getProjectId());
        params.put("procurementType", controlPurchasePlan.getProcurementType());
        List<GcControlPlanList> gcControlPlanLists = gcControlPlanListMapper.selectGcControlPlanByTwoIdList(params);
        controlPurchasePlan.setGcControlPlanList(gcControlPlanLists);


        String procurementType = controlPurchasePlan.getProcurementType();
        String dictLabel;
        if (procurementType.equals("0")){
            dictLabel="货物类";
        }else if (procurementType.equals("1")){
            dictLabel="施工类";
        }else {
            dictLabel="服务类";
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", gcControlPlanLists);
        map.put("projectName", controlPurchasePlan.getProjectName());
        map.put("purchasePlanName", controlPurchasePlan.getPurchasePlanName());
        map.put("procurementType", dictLabel);
        try {
            TemplateExcelUtils.export("采购计划.xlsx","采购计划清单",map,response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取成本控制--采购计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:purchase:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(gcControlPurchasePlanService.selectGcControlPurchasePlanById(id));
    }

    /**
     * 新增成本控制--采购计划
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:purchase:add')")
    @Log(title = "成本控制--采购计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GcControlPurchasePlan gcControlPurchasePlan) {
        return success(gcControlPurchasePlanService.insertGcControlPurchasePlan(gcControlPurchasePlan));
    }

    /**
     * 修改成本控制--采购计划
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:purchase:edit')")
    @Log(title = "成本控制--采购计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GcControlPurchasePlan gcControlPurchasePlan) {
        return toAjax(gcControlPurchasePlanService.updateGcControlPurchasePlan(gcControlPurchasePlan));
    }

    /**
     * 删除成本控制--采购计划
     */
    @PreAuthorize("@ss.hasPermi('costcontrol:purchase:remove')")
    @Log(title = "成本控制--采购计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gcControlPurchasePlanService.deleteGcControlPurchasePlanByIds(ids));
    }
}
